//
//  MLSWelcomeController.swift
//  MLSMircoBlog
//
//  Created by 袁航 on 15/7/31.
//  Copyright © 2015年 yuanhang. All rights reserved.
//

import UIKit
import SDWebImage
class MLSWelcomeController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置背景色
        view.backgroundColor = UIColor.whiteColor()
        // 设置UI
        setUpUI()
        // 设置头像
        if let urlStr = MLSAccessToken.shareAccessToken?.avatar_large {
            headImage.sd_setImageWithURL(NSURL(string: urlStr))
            // 卡顿0.3秒,保证如果有网络的情况下,能够下载完图像(开发中没有必要)
            NSThread.sleepForTimeInterval(0.3)
        }
    }
    //MARK: - 动画
    // 定义变量,记录动画约束
    private var animationCons: NSLayoutConstraint?
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 改变约束,(不会立刻实现)
        animationCons!.constant = -(CGRectGetMaxY(self.headImage.frame) )
        ///  动画
        ///  - parameter <Tduration            : 持续时间
        ///  - parameter delay                 : 延时多长时间播放
        ///  - parameter usingSpringWithDamping: 弹性力度
        ///  - parameter initialSpringVelocity : 初始速度
        ///  - parameter options               : 动画效果选项
        ///  - parameter animations            : 动画属性
        ///  - parameter completion            : 结束后闭包
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            // 强制重新布局
            self.view.layoutIfNeeded()
            
            }) { (result) -> Void in
                
                // 名称动画
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.nameLabel.transform = CGAffineTransformIdentity
                    }, completion: { (result) -> Void in
                        // 动画完成,跳转控制器
                        if result {
                            NSThread.sleepForTimeInterval(0.2)
                            // 跳转控制器(发送通知)
                            NSNotificationCenter.defaultCenter().postNotificationName(MLSChangeRootviewControllerNotifaction, object: self, userInfo: [MLSChangeRootVCNotiUserInfoKey: MLSMainController()])
                        }
                })
                
        }
    }
    
    //MARK: - 自动布局
    private func setUpUI() {
        backImage.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(backImage)
        headImage.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(headImage)
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(nameLabel)
        
        ///  背景图片约束
        backImage.ff_Fill(view)
        ///  头像约束
        let cons = headImage.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -180))
        animationCons = headImage.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        ///  提示文字约束
        nameLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: headImage, size: nil, offset: CGPoint(x: 0, y: 16))
        
    }
    
    
    //MARK: - 懒加载控件
    /// 背景图片
    private lazy var backImage: UIImageView = {
        return UIImageView(image: UIImage(named: "ad_background"))
        }()
    ///  头像
    private lazy var headImage: UIImageView = {
        let headImage = UIImageView(image: UIImage(named: "avatar_default_big"))
        headImage.clipsToBounds = true
        headImage.layer.cornerRadius = 45
        return headImage
        }()
    /// 提示文字
    private lazy var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.transform = CGAffineTransformMakeScale(0, 0)
        if let name = MLSAccessToken.shareAccessToken?.screen_name {
            nameLabel.text = "\(name)\n欢迎归来"
        } else {
            nameLabel.text = "欢迎归来"
        }
        // 行数为2行
        nameLabel.numberOfLines = 2
        // 居中显示
        nameLabel.textAlignment = NSTextAlignment.Center
        // 自动填充尺寸
        nameLabel.sizeToFit()
        return nameLabel
        }()
}
